<template>
  <div class="app-container zombies">
    <div v-if="!showJobs">
      <el-table
        :data="list"
        style="width: 100%">
        <el-table-column
          prop="name"
          label="节点">
          <template slot-scope="scope">
            <span>{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="gpu"
          label="GPU">
          <template slot-scope="scope">
            <span>{{ scope.row.gpu ? scope.row.gpu : '-' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100" align="center">
          <template slot-scope="scope">
            <a class="table-action" @click="viewJobs(scope.row)">查看任务</a>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div v-show="showJobs">
      <div class="jobs-header">
        <span>节点：{{ curViewZombie }}</span>
        <a class="view-jobs-back" @click="returnBack">返回</a>
      </div>
      <el-table
        :data="zombiesJobs"
        fit
        highlight-current-row>
        <el-table-column align="left" label="任务名">
          <template slot-scope="scope">
            {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column align="left" label="用户名">
          <template slot-scope="scope">
            {{ scope.row.userName }}
          </template>
        </el-table-column>
        <el-table-column align="left" label="GPU">
          <template slot-scope="scope">
            {{ scope.row.gpu.length > 0 ? scope.row.gpu.toString() : '-' }}
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { getContainerZombies } from '@/api/analysis'

export default {
  data() {
    return {
      list: null,
      listLoading: true,
      showJobs: false,
      zombiesJobs: [],
      curViewZombie: ''
    }
  },
  created() {
    this._getContainerZombies()
  },
  methods: {
    // 查看任务
    viewJobs(row) {
      this.curViewZombie = row.name
      this.zombiesJobs = row.jobs
      this.showJobs = true
    },
    // 查看僵尸容器任务返回
    returnBack() {
      this.curViewZombie = ''
      this.zombiesJobs = []
      this.showJobs = false
    },
    // 查询重启面板
    _getContainerZombies() {
      this.listLoading = true
      getContainerZombies().then(res => {
        if (res.message.code === 0) {
          this.listLoading = false
          this.list = []
          res.data && Object.keys(res.data).forEach(key => {
            const nodeItem = {
              name: key,
              gpu: res.data[key].gpu.toString(),
              jobs: res.data[key].jobs
            }
            this.list.push(nodeItem)
          })
        } else {
          this.$message({ message: res.message.message, showClose: true, type: 'error' })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.zombies {
  &-header {
    padding: 10px;
    background: #ffffff;
    .header-title {
      font-size: 14px;
      font-weight: 700;
    }
    .header-title-left {
      float: right;
      height: 40px;
      line-height: 40px;
    }
  }
  .jobs-header {
    padding: 10px;
    background: #ffffff;
    font-size: 14px;
    color: #303133;
    font-weight: bold;
  }
  .jobs-header .view-jobs-back {
    float: right;
    color: #3a8ee6;
    font-weight: normal;
  }
  label {
    font-weight: normal
  }
  .table-action {
    color: #4285f4;
  }
  .table-link {
    color: #4285f4;
    &:hover {
      cursor: pointer;
    }
  }
}
</style>
